using System.Collections.Generic;
using UnityEngine;

namespace ai_library
{
	class EntityManager
	{
		private static EntityManager instance = new EntityManager();

		private Dictionary<int, IEntity> m_entities = new Dictionary<int, IEntity>();

		private EntityManager()
		{
		}

		public static EntityManager Instance
		{
			get
			{
				return instance;
			}
		}

		public int EntityCount
		{
			get
			{
				return m_entities.Count;
			}
		}

		public bool RegisterAgent(IEntity IEntity)
		{
			if (!m_entities.ContainsKey(IEntity.ID))
			{
				m_entities.Add(IEntity.ID, IEntity);
				return true;
			}

			return false;
		}

		public bool RemoveAgent(IEntity IEntity)
		{
			if (m_entities.ContainsKey(IEntity.ID))
			{
				m_entities.Remove(IEntity.ID);
				return true;
			}

			return false;
		}

		public bool IsAgentRegistered(IEntity IEntity)
		{
			return m_entities.ContainsKey(IEntity.ID);
		}

		public bool IsAgentRegistered(int id)
		{
			return m_entities.ContainsKey(id);
		}

		public bool GetAgentFromKey(int key, out IEntity IEntity)
		{
			return m_entities.TryGetValue(key, out IEntity);
		}

		public int FindRandomAgent(params int[] exludeId)
		{
			int key = -1;

			Dictionary<int, IEntity>.KeyCollection keyCollection = m_entities.Keys;

			List<int> keys = new List<int>(keyCollection);

			foreach( int i in exludeId)
			{
				keys.Remove(i);
			}

			if (keys.Count > 0)
			{
				key = keys[ Random.Range (0, keys.Count - 1) ];
			}

			return key;
		}
	}
}

